﻿using AirlineBookingSystem.Bookings.Core.Entities;
using AirlineBookingSystem.Bookings.Core.Repositories;
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace AirlineBookingSystem.Bookings.Infrastructure.Repositories
{
    public class BookRepositoryRedis : IBookRepository
    {
        private readonly IDatabase _database;
        private const string RedisKeyPrefix = "booking_";

        public BookRepositoryRedis(IDatabase database)
        {
            _database = database;
        }



        public async Task AddBookingAsync(Booking booking)
        {
            var data = JsonConvert.SerializeObject(booking);
            await _database.StringSetAsync($"{RedisKeyPrefix}{booking.Id}", data);

        }

        public async Task<Booking> GetBookingByIdAsync(Guid id)
        {
            var data = await _database.StringGetAsync($"{RedisKeyPrefix}{id}}");
            return string.IsNullOrEmpty(data) ? null : JsonConvert.DeserializeObject<Booking>(data);
        }
    }
}
